round
¶round(3.14159, 2)
3.14
round(3.14159, 4)
3.1416
apples = 7
people = 3
per_person = apples / people
print(f'Each person gets { per_person } apples')
Each person gets 2.3333333333333335 apples
apples = 7
people = 3
per_person = round(apples / people, 1)
print(f'Each person gets { per_person } apples')
Each person gets 2.3 apples
None
¶What is the value of a variable that doesn't have a value?
What does a function return when it doesn't return
anything?
def no_return():
print('This function doesn\'t return anything.')
value = no_return()
print(value)
This function doesn't return anything. None
type(value)
NoneType
None
is what Python uses to communicate nothing.
It's what you use to indicate that you don't have any information.
thing = 8
thing is None
False
thing = None
thing is None
True
To determine whether a variable is None
, use the is None
expression.
thing = 9
thing is not None
True
To determine whether a variable is not None
, use the is not None
expression.
tuple
¶info = ('Gordon', 'Bean', 38)
A tuple
is a collection of data, like a list
.
A list
stores a sequence of data that has the same role or quality.
['John', 'Mary', 'Susan', 'David']
A tuple
stores distinct pieces of information that belong together as a larger unit.
('John', 'Doe', 21)
first, last, age = info
print(f'{last}, {first} ({age})')
Bean, Gordon (38)
print(info)
('Gordon', 'Bean', 38)
We access the pieces of a tuple
using unpacking.
The first item in the tuple is assigned to the first variable, the second item to the second variable, etc.
def get_number_pair():
first = int(input('First number: '))
second = int(input('Second number: '))
return first, second
print('First pair')
a, b = get_number_pair()`
print('Second pair')
x, y = get_number_pair()
if a + b > x + y:
print('The first pair is bigger')
else:
print('The second pair is bigger (or they are equal)')
First pair First number: 7 Second number: 4 Second pair First number: 69 Second number: -22 The second pair is bigger (or they are equal)
registration.py
¶get_participant
None
to communicate "no person"(first, last, age)
register_participants
get_participant
print_participants
meal_planner.py
¶Write a program that builds up a meal plan for several days.
An individual meal needs a grain, vegetable, and fruit.
Allow the user to plan out as many meals as they want.
Then print out the planned meals.
Plan a meal Grain: rice Vegetable: broccoli Fruit: strawberry Plan a meal Grain: pasta Vegetable: peas Fruit: cranberry Plan a meal Grain: bread Vegetable: carrots Fruit: apples Plan a meal Grain: You planned 3 meals: Grain: rice, Veggy: broccoli, Fruit: strawberry Grain: pasta, Veggy: peas, Fruit: cranberry Grain: bread, Veggy: carrots, Fruit: apples
None
tuple